#!/bin/sh
# shell script for automating musictracker project releases
# $1 = 'release' or 'snapshot'
# $2 = version number for release

# assign a version number
if [ $1 != "release" ]
then
 # get trunk svn revision
 SVN_REV=`svn info https://pidgin-musictracker.googlecode.com/svn/trunk | grep Revision | cut -d':' -f2 | tr -d ' '`

 # obtaining svn revision failed, do nothing...
 if [ -z $SVN_REV ] 
 then
   echo "Couldn't get svn rev"
   exit 0
 fi

 # don't do anything if it hasn't changed since last time we ran
 if [ `cat ~/.pidgin-musictracker-subversion-revision` = $SVN_REV ]
 then
   exit 0
 fi
 echo "Trunk is at svn revision $SVN_REV"

 export SNAPSHOT=`date -u +%Y%m%d`svn$SVN_REV
 VERSION=snapshot-$SNAPSHOT
 WORKDIR=pidgin-musictracker-snapshot
else
 VERSION=$2
 WORKDIR=pidgin-musictracker-$VERSION
fi

# clean
mkdir -p musictracker-package-release
cd musictracker-package-release
rm -rf *

# export source from svn
if [ $1 != "release" ]
then
 svn export https://pidgin-musictracker.googlecode.com/svn/trunk $WORKDIR
else
 svn copy https://pidgin-musictracker.googlecode.com/svn/trunk/ https://pidgin-musictracker.googlecode.com/svn/tags/$VERSION -m "Tagging release version $VERSION"
 svn export https://pidgin-musictracker.googlecode.com/svn/tags/$VERSION $WORKDIR
fi

# record version number
echo $VERSION >$WORKDIR/version

# run autotools
(cd $WORKDIR ; ./autogen.sh)

# generate .tar.bz2 file
tar -cjf pidgin-musictracker-$VERSION.tar.bz2 $WORKDIR

# build win32
# (this symlink is needed as we depend on pidgin source for some make infrastructure)
ln -s ~/pidgin pidgin
# (install local.mak into pidgin source tree)
cp $WORKDIR/packaging/local.mak pidgin/
(cd $WORKDIR ; make -f Makefile.mingw installer)

# build linux
(cd $WORKDIR/ ; ./configure --prefix=/usr ; make)
tar -cjf pidgin-musictracker-$VERSION-bin.tar.bz2 -C ./$WORKDIR/src/.libs musictracker.so

if [ $1 != "release" ]
then
 # upload files to my webhost
 scp $UPLOAD_CREDENTIALS \
     pidgin-musictracker-$VERSION-bin.tar.bz2 \
     $WORKDIR/installer/pidgin-musictracker-$VERSION.exe \
     pidgin-musictracker-$VERSION.tar.bz2 \
     $UPLOAD_TARGET/pidgin-musictracker-snapshot

 rm -r $WORKDIR
 # request a build on launchpad
 ~/launchpad-ppa
 # request a build on koji
 ~/fedora-koji

 # remember this svn revision number for checking when it changes
 echo $SVN_REV >~/.pidgin-musictracker-subversion-revision
else
 #  use googlecode_upload.py to upload the files
 googlecode_upload.py -s "Linux x86 plugin binary"  -p pidgin-musictracker -l          Beta,OpSys-Linux,Type-Executable  pidgin-musictracker-$VERSION-bin.tar.bz2
 googlecode_upload.py -s "Windows plugin installer" -p pidgin-musictracker -l Featured,Beta,OpSys-Windows,Type-Installer $WORKDIR/installer/pidgin-musictracker-$VERSION.exe
 googlecode_upload.py -s "Source package"           -p pidgin-musictracker -l Featured,Beta,OpSys-All,Type-Source        pidgin-musictracker-$VERSION.tar.bz2

 # upload source tarball to release area on my webhost as well
 scp $UPLOAD_CREDENTIALS \
     pidgin-musictracker-$VERSION.tar.bz2 \
     $UPLOAD_TARGET/pidgin-musictracker

 # remember this version number to use as the base for snapshot package names
 echo $VERSION >~/.pidgin-musictracker-release-version
fi

